﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Composition;

namespace WindowsFormsApplication2
{
    [Export(typeof(IBrowser))]
    public partial class Form1 : Form, IBrowser
    {
        [ImportMany(typeof(IForm))]
        IEnumerable<IForm> forms;
        
        public Form1()
        {
            InitializeComponent();
        }

           private void Form1_Load(object sender, EventArgs e)
        {
            foreach (var form in forms)
            {
               
                listView1.Items.Add(string.Format("{0} - {1}", form.FormCode, form.FormName));
            }
        }

        public Form MainForm()
        {
            return this;
        }

        public void ShowForm(string formCode)
        {
            IForm form = forms.First(f=>f.FormCode == formCode);
        
            var page = new TabPage()
             {
                 Text = form.FormName,
                
             };
            form.Form().Dock = DockStyle.Fill;
            page.Controls.Add(form.Form());
            tabControl1.TabPages.Add(page);
        }

  

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            string[] form = listView1.SelectedItems[0].Text.Split(new char[] { '-' });
            ShowForm(form[0].Trim());
        }     
    }
}
